#!/usr/bin/env ruby
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
# frozen_string_literal: true

require 'set'
require_relative '../defconfig'

docker_rm 'send-internet-mail'

names = Set.new %w[
  SMTP_ADDRESS
  SMTP_PORT
  SMTP_DOMAIN
  ROBOT_EMAIL_ADDRESS
  ROBOT_EMAIL_PASSWORD
  SEND_MAIL_PORT
  SENT_MAILDIR
  REDIS_PORT
  EMAIL_LIMIT_COUNT
]

defaults = relevant_defaults(names)

defaults['CCI_SRC'] = '/c/compass-ci'
defaults['SENT_MAILDIR'] ||= '/srv/cci/Maildir/.sent'
defaults['HOST_SERVER'] = ENV['HOSTNAME']

env = docker_env(defaults)

cmd = %w[
  docker run
  --restart=always
  --name=send-internet-mail
  -d
] + env + %W[
  -p #{defaults['SEND_MAIL_PORT']}:#{defaults['SEND_MAIL_PORT']}
  -v #{defaults['SENT_MAILDIR']}:#{defaults['SENT_MAILDIR']}:rw
  -v #{ENV['CCI_SRC']}:#{defaults['CCI_SRC']}
  -v /etc/localtime:/etc/localtime:ro
  -w #{defaults['CCI_SRC']}/container/send-internet-mail
  send-internet-mail
]

cmd += ['ruby', 'send-internet-mail.rb']

system(*cmd)
